﻿function AzureOnlineQueueMessageDeleteDetailsView(context) {

    var self = this;
    
    var spinner = new SpinnerModel();
    
    var dispatcher = $({});

    var queue = context.queues.queue;
    
    var messages = context.queues.messages;
    
    var msg;

    function init(message) {
        msg = message;
    }

    function remove() {
        var queueName = queue();
        var mid = msg.id();
        spinner.enter();
        var api = context.api();
        api.queues.getMessages({
            queue: queueName,
            visibilityTimeout: 5,
            success: function (raw) {
                spinner.leave();
                
                var $doc = $(raw);

                var $items = $doc.find('QueueMessagesList QueueMessage');
                
                var found = false;
                for (var i = 0; i < $items.length; i++) {
                    var msgRawItem = $items[i];
                    var msgItem = new AzureOnlineQueueMessage($(msgRawItem));
                    if (msgItem.id() == mid) {
                        spinner.enter();
                        api.queues.deleteMessage({
                            queue: queueName,
                            messageId: mid,
                            popreceipt: msgItem.popreceipt(),
                            success: function () {
                                spinner.leave();
                                messages.remove(msg);
                                dispatcher.trigger('success');
                            },
                            error: function () {
                                spinner.leave();
                                return 'notify';
                            }
                        });
                        found = true;
                        break;
                    }
                }
            },
            error: function () {
                spinner.leave();
            }
        });
    }

    function addOnSuccess(handler) {
        dispatcher.bind('success', handler);
    };

    self.init = init;
    self.queue = queue;
    self.remove = remove;
    self.addOnSuccess = addOnSuccess;
    
}

function AzureOnlineQueueMessageDeleteAllDetailsView(context) {

    var self = this;

    var spinner = new SpinnerModel();

    var dispatcher = $({});

    var queue = context.queues.queue;

    var messages = context.queues.messages;

    function init() {
    }

    function remove() {
        var queueName = queue();
        spinner.enter();
        var api = context.api();
        api.queues.clearMessages({
            queue: queueName,
            success: function () {
                spinner.leave();
                messages([]);
                dispatcher.trigger('success');
            },
            error: function () {
                spinner.leave();
                dispatcher.trigger('success');
            }
        });
    }

    function addOnSuccess(handler) {
        dispatcher.bind('success', handler);
    };

    self.init = init;
    self.queue = queue;
    self.remove = remove;
    self.addOnSuccess = addOnSuccess;

}